package suanfa.lianxi;

public class Student {

    public int size = 0;
    public String[] name;
    public int[] p;

    public Student() {
        this.name = new String[100];
        this.p = new int[100];
    }

    public void addStudent(String name1, int i) {
        this.name[size] = name1;
        this.p[size] = i;

        System.out.println(name[size] + " " + p[size]);
        size++;

    }

    public void find() {
        int max = 0;
        int min = 100;
        int mx = 0;
        int mi = 0;
        for (int i = 0; i < size; i++) {
            if (p[i] > max && p[i] != 0) {
                max = p[i];
                mx = i;
            }
            if (p[i] < min && p[i] != 0) {
                mi = i;
                min = p[i];
            }
        }
        System.out.println("分最高的：" + name[mx]);
        System.out.println("分最低的：" + name[mi]);
    }

    public void delete(String n) {
        int s = 0;
        for (int i = 0; i < size; i++) {//找下标
            if (name[i] == n) {
                s = i;
            }
        }
        for (int i = s; i <= size - 1; i++) {
            name[i] = name[i + 1];
            p[i] = p[i + 1];
        }
        size--;

    }

    public void insertweibu(String n, int data) {

        name[size] = n;
        p[size] = data;
        size++;
    }

    public void paixu() {//从低到高排，用冒泡
        for (int i = 0; i <size-1; i++) {
            for(int j=0;j<size-i-1;j++){
                if(p[j]>p[j+1]){
                    int o=p[j];
                    String l=name[j];
                    p[j]=p[j+1];
                    p[j+1]=o;
                    name[j]=name[j+1];
                    name[j+1]=l;
                }
            }
        }
        System.out.println("从大到小排序为");
        for(int i=0;i<size;i++){
            System.out.println(name[i]+" "+p[i]);
        }
    }

    public void update(String n,int pl){
        int o=0;
        for(int i=0;i<size;i++){
            if(name[i]==n){
                o=i;//记录下标
            }
        }
        p[o]=pl;
        System.out.println("已修改"+name[o]+"的成绩为"+p[o]);
    }


    public static void main(String[] args) {
        Student s=new Student();
        s.addStudent("同学1",95);
        s.addStudent("同学2",90);
        s.addStudent("同学3",100);
        s.delete("同学2");
        s.insertweibu("同学4",70);
        System.out.println("------------");
        s.find();
        s.update("同学1",85);
        System.out.println("------------");
        s.paixu();



    }
    }

